<?

class Image{ 

    var $src_filename; 

    var $src_width;

    var $src_height; 

    var $src_type; 

    var $src_attr; 

    var $src_image; 
	
	var $name; 



    function Image($filename){ 

        $this->src_filename = $filename; 

        $this->GetImageInfo();
        
        $this->CreateSourceImage();

    } 



    function GetImageInfo(){ 

        list($this->src_width,$this->src_height, $this->src_type, $this->src_attr) = getimagesize($this->src_filename); 

    } 



    function CreateSourceImage(){ 

        switch($this->src_type){ 

            case 1: 

                $this->src_image =imagecreatefromgif($this->src_filename); 

                   break; 

            case 2: 

                $this->src_image =imagecreatefromjpeg($this->src_filename); 

            break; 

            case 3: 

                $this->src_image =imagecreatefrompng($this->src_filename); 

            break; 

            default:    return false; 

        } 



        return true; 

    } 



	function SaveProportionateImage($filename, $quality, $height, $width){
      if($height=="0")  {
		$dest_width = $width;
        $ratio = $this->src_width / $width;
        $dest_height = $this->src_height / $ratio;
	   } else {
		   $dest_height = $height;
           $ratio = $this->src_height / $height;
           $dest_width = $this->src_width / $ratio;
	   }
		
        $dest_image = imagecreatetruecolor($dest_width,$dest_height);

        imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0, 

            $dest_width,

            $dest_height,

            $this->src_width, 

            $this->src_height); 

        switch($this->src_type){ 

            case IMG_GIF:
            if(function_exists(imagegif))
             imagegif($dest_image, $filename, $quality);
            break;

            case IMG_JPG:
            if(function_exists(imagejpeg)) {
             //die("Errore!!!");
             imagejpeg($dest_image, $filename, $quality); }
            break;

            case IMG_PNG:
            if(function_exists(imagepng))
             imagepng($dest_image, $filename, $quality);
            break;
            
            case IMG_WBMP:
            if(function_exists(imagewbmp))
             imagewbmp($dest_image, $filename, $quality);
            break;

            default:
             return false;
            break;

        }
         
        imagedestroy($dest_image);
		return true;
    }
    
	function SaveProportionateImage2($filename, $quality, $height, $width){
        if($height=="0")  {
        	$dest_width = $width;
        	$ratio = $this->src_width / $width;
        	$dest_height = $this->src_height / $ratio;
        } else {
        	$dest_height = $height;
        	$ratio = $this->src_height / $height;
        	$dest_width = $this->src_width / $ratio;
        }
        $dest_image = imagecreatetruecolor($dest_width,$dest_height);
        imagecopyresampled($dest_image, $this->src_image, 0, 0, 0, 0, $dest_width,$dest_height,$this->src_width,$this->src_height); 

        switch($this->src_type){ 

            case 1:
            if(function_exists(imagegif)) {
             imagegif($dest_image, $filename, $quality);
             chmod($filename,0755);
            }
            break;

            case 2:
            if(function_exists(imagejpeg)) {
             //die("Errore!!!");
             imagejpeg($dest_image, $filename, $quality); 
             chmod($filename,0755);
            }
            break;

            case 3:
            if(function_exists(imagepng)) {
             imagepng($dest_image, $filename, $quality);
             chmod($filename,0755);
            }
            break;

            default:
             return false;
            break;

        }
         
        imagedestroy($dest_image);

    }



    function Free(){ 

        imagedestroy($this->src_image); 

    }
    
    function CheckSizeFormat($file,$width,$height,$tipo="") {
        global $path_tmp;
		  if($this->src_width <= $width && $this->src_height <= $height) {
		    $this->name = $file["name"];
		    //faccio l'upload in tmp
		    $newFile = new FileUpload($path_tmp);
		    $newFile->Upload($file);
        	$error = "";
		  } else
		     $error = "Controlla la dimensione dell'immagine $tipo.<br>";  
	  return $error;  
	}

} 
?>